﻿using API_Service.DBHelper;
using Dapper;
using Newtonsoft.Json;
using System;
using System.Data;
using System.Linq;
using System.Reflection;

namespace API_Service.BizLogic.WIPControl
{
    public class WIPTrackingService:BaseLogicService
    {

        public ExecutionResult<dynamic> UpdateWIPTrackingInf0(dynamic vModel)
        {
            var result = new ExecutionResult<dynamic>();

            return result;
        }

        public ExecutionResult<dynamic> GetWIPTrackingInfo(dynamic vQCondition)
        {
            ExecutionResult<dynamic> result;
            result = new ExecutionResult<dynamic>();
            var dbParams = new DynamicParameters();
            #region SQL and DB parameters.
            string sql = @"SELECT  *  FROM [dbo].[r_wip_tracking_t] WHERE 1=1";
            if (!String.IsNullOrEmpty(vQCondition.sn))
            {
                sql = sql + " AND [serial_number]=@sn";
                dbParams.Add("@sn", (string)vQCondition.sn, DbType.String, ParameterDirection.Input);
            }
            if (!String.IsNullOrEmpty(vQCondition.mo))
            {
                sql = sql + " AND [mo_number]=@mo";
                dbParams.Add("@mo", (string)vQCondition.mo, DbType.String, ParameterDirection.Input);
            }
            if (!String.IsNullOrEmpty(vQCondition.model))
            {
                sql = sql + " AND [model_name]=@model";
                dbParams.Add("@model", (string)vQCondition.model, DbType.String, ParameterDirection.Input);
            }
            if (!String.IsNullOrEmpty(vQCondition.sku))
            {
                sql = sql + " AND [key_part_no]=@sku";
                dbParams.Add("@sku", (string)vQCondition.sku, DbType.String, ParameterDirection.Input);
            }
            if (!String.IsNullOrEmpty(vQCondition.group))
            {
                sql = sql + " AND [group_name]=@group_name";
                dbParams.Add("@group_name", (string)vQCondition.group_name, DbType.String, ParameterDirection.Input);
            }
            if (!String.IsNullOrEmpty(vQCondition.start_time) && !String.IsNullOrEmpty(vQCondition.end_time))
            {
                sql = sql + " AND [in_station_time] BETWEEN @start_time AND @end_time";
                dbParams.Add("@start_time", (DateTime)vQCondition.start_time, DbType.DateTime, ParameterDirection.Input);
                dbParams.Add("@end_time", (DateTime)vQCondition.end_time, DbType.DateTime, ParameterDirection.Input);
            }
            if (!String.IsNullOrEmpty(vQCondition.line))
            {
                sql = sql + " AND [line_name] =@line";
                dbParams.Add("@line", (string)vQCondition.line, DbType.String, ParameterDirection.Input);
            }
            #endregion
            using(IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    result.Datas = conn.Query(sql, dbParams).ToArray();
                    result.Status = true;
                    result.Message = "查询WIP信息成功.";
                    Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + "-" + result.Message);
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "查询WIP信息异常。错误信息：" + ex.Message;
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);                    
                }
            }
            Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + "-->" + result.Message);

            return result;
        }

        public ExecutionResult<dynamic> CheckSNExists(string vSN)
        {
            var result = new ExecutionResult<dynamic>();

            result = this.QueryWIPTrackingInfo(vSN);
            if (result.Status && result.Datas.Length > 0)
            {
                result.Status = true;
                result.Message = "SN:" + vSN + "已经存在";
            }
            Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + "-->" + result.Message);
            return result;
        }        

        private ExecutionResult<dynamic> QueryWIPTrackingInfo(string vSN)
        {
            ExecutionResult<dynamic> result;
            result = new ExecutionResult<dynamic>();
            string sql = @"SELECT  *  FROM [dbo].[r_wip_tracking_t] WHERE [serial_number] =@sn";
            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    result.Datas = conn.Query(sql, new { sn = vSN }).ToArray();
                    result.Status = true;
                    result.Message = "查询MES WIP信息成功.";
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "检查SN时异常。";
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }
            }
            Logger.WriteDebug(MethodBase.GetCurrentMethod().Name + "-->" + result.Message);

            return result;
        }
        
    }
}